package com.nf.health.app.activity.home;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.home.adapter.NewHomeAdapter;
import com.nf.health.app.activity.home.bean.MyBinnerHomeModel;
import com.nf.health.app.activity.home.bean.MyHomeHospitalConsultationModel;
import com.nf.health.app.core.MyGsonBuilder;
import com.nf.health.app.http.Act;
import com.nf.health.app.impl.event.EventContact;
import com.nf.health.app.impl.event.EventType;
import com.nf.health.app.models.DoctorVo;
import com.nf.health.app.models.HealthStateInfo;
import com.nf.health.app.models.HospatalStateBean;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.GetToast;
import com.nf.health.app.utils.JsonUtils;
import com.nf.health.app.utils.NetUtil;
import com.nf.health.app.utils.ViewUtil;
import com.nf.health.app.widget.MyMainItemAnimation;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 *首页
 * 注意：  进入这个界面----医院详情的时候，要传入hid，
 * 目前只有启动这个界面的有：  我的医院列表
 */
public class MainHomeActivity extends BaseActivity {
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.titlebar_right_view)
    ImageView titlebar_right_view;
    @Bind(R.id.titlebar_title_tv)
    TextView titlebar_title_tv;
    NewHomeAdapter homePageAdapter;
    private List<HealthStateInfo> list;
    public static String  DOCTOR_DETAIL_HID = "com.hotipal.hid";
    public static String hid = "";
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContent(R.layout.layout_new_home_recycleview);
        setTitleBar(R.layout.titlebar_base);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initView();
        initListener();
        loadMianData();
    }

    private void initView() {
        hid = getIntent().getStringExtra(DOCTOR_DETAIL_HID);
        list = new ArrayList<>();
        swipeRefreshLayout.setColorSchemeResources(R.color.theme_color, android.R.color.holo_red_light, android.R.color.holo_green_light, android.R.color.holo_orange_light);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new MyMainItemAnimation());
        homePageAdapter = new NewHomeAdapter(MainHomeActivity.this,new HealthStateInfo(),hid);
        recyclerView.setAdapter(homePageAdapter);
        titlebar_right_view.setImageResource(R.drawable.iv_take_a_warning);
        ViewUtil.setText(this, R.id.titlebar_title_tv,"医院");

    }
    private void loadMianData() {
        provider.requestHospatalState(Act.Flag.FLAG_HOSPATAL_STATE,hid);//获取医院图片  挂号状态
        provider.getYLHBanner(hid);//请求.轮播图
        provider.requestDoetorAdkConluSion("1", "6",hid);//医院资讯
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        switch (actionName){
            case Act.Flag.FLAG_GETHOME_YLHBBANNER://首页的导航
                List<MyBinnerHomeModel> listBean = (List<MyBinnerHomeModel>) object;
                homePageAdapter.setBinnerListContentMsg(listBean);
                break;
            case Act.Flag.FLAG_DOCTOR_ASK_ZX_LIST://医生资讯列表
                List<MyHomeHospitalConsultationModel> hospitalInformationCinlusionModeList = (List<MyHomeHospitalConsultationModel>) object;
                homePageAdapter.sethospitalInformationCinlusionModeList(hospitalInformationCinlusionModeList);
                break;
            case Act.Flag.SEARCH_DOCTOR_KESHI_HOSTIPAL_FLG://名医专家列表
                String data = JsonUtils.getFiledData(object.toString(), "data");
                String list = JsonUtils.getFiledData(data,"list");
                Gson gson = new MyGsonBuilder().createGson();
                List<DoctorVo> myDoctor = gson.fromJson(list, new TypeToken<List<DoctorVo>>() {
                }.getType());
                 // homePageAdapter.setDoctorAskConsulision(myDoctor);
                break;
            case Act.Flag.FLAG_HOSPATAL_STATE://蒙医文化大图片 和 预约挂号 院内导航，，满意度调查的状态
                Gson gson1 = new MyGsonBuilder().createGson();
                String data1 = JsonUtils.getFiledData(object.toString(), "data");
                HospatalStateBean hospatalStateBean = gson1.fromJson(data1,new TypeToken<HospatalStateBean>() {}.getType());
                homePageAdapter.setHospitalConsulision(hospatalStateBean);
                break;
        }
    }

    @Subscribe(threadMode= ThreadMode.MAIN,sticky = true)
    public void mainOnThreadRefreshUi(EventType eventType){
        int type = eventType.getType();
        switch (type){
            case EventContact.QUESTION_ADK://
              //  provider.hourseClearTakeCompete();//
                break;
        }
    }
    private void initListener() {
        titlebar_right_view.setOnClickListener(this);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefreshLayout.setRefreshing(true);
                if(NetUtil.isNetworkAvailable(MainHomeActivity.this)){
                    loadMianData();
                }else{
                    GetToast.useString(MainHomeActivity.this, getString(R.string.l_no_connect_err));
                }

            }
        });
    }

    @Override
    public void handleActionFinish(String actionName, Object object) {
        super.handleActionFinish(actionName, object);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.titlebar_right_view://参看信息公告
                Bundle bundle2 = new Bundle();
                bundle2.putString(InformationBoardActivity.HID_FLAG,hid);
                ActivityUtils.showActivity(MainHomeActivity.this, InformationBoardActivity.class,bundle2);
                break;
        }
    }
    @Override
    public void onStart() {
        super.onStart();
//        homePageAdapter.setAutoScroll(true);
    }

    @Override
    public void onStop() {
        super.onStop();
//        homePageAdapter.setAutoScroll(false);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}